<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>微信消息管理</title>
	<meta name="decorator" content="default"/>
	<%--<%@include file="/WEB-INF/views/include/chosen.jsp" %>--%>
	<link href="${ctxStatic}/modules/chosen/css/bootstrap.min.css" rel="stylesheet">
	<link href="${ctxStatic}/modules/chosen/dist/css/component-chosen.css" rel="stylesheet">
	<style>
		.search-field input{
			outline: none;
		}
		#inputForm{
			overflow: hidden;
		}
		#controls{
			height:300px;
		}
		.control-group{
			height:auto;
		}
		.chosen-choices{
			width: 460px!important;
			height:150px!important;
		}
	</style>
	<script type="text/javascript">
		$(document).ready(function() {
			// //$("#name").focus();
			// $("#inputForm").validate({
			// 	submitHandler: function(form){
			// 		loading('正在提交，请稍等...');
			// 		form.submit();
			// 	},
			// 	errorContainer: "#messageBox",
			// 	errorPlacement: function(error, element) {
			// 		$("#messageBox").text("输入有误，请先更正。");
			// 		if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
			// 			error.appendTo(element.parent().parent());
			// 		} else {
			// 			error.insertAfter(element);
			// 		}
			// 	}
			// });
		});
	</script>
</head>
<body scroll="no">
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/wechatmsg/weixinMsg/">微信消息列表</a></li>
		<li class="active"><a href="${ctx}/wechatmsg/weixinMsg/form?id=${weixinMsg.id}">微信消息<shiro:hasPermission name="wechatmsg:weixinMsg:edit">${not empty weixinMsg.id?'修改':'发送'}</shiro:hasPermission><shiro:lacksPermission name="wechatmsg:weixinMsg:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<div style="color: red ;margin-left: 20px;font-size: medium;margin-top: -10px ;padding-bottom: 20px">群发消息规则：根据微信官方规定，用户每月只能接收4条群发消息，多于4条的群发将对该用户发送失败；</div>
	<form:form id="inputForm" modelAttribute="weixinMsg" action="${ctx}/wechatmsg/weixinMsg/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
		<div class="control-group">
			<label class="control-label">标题：</label>
			<div class="controls">
				<form:input path="title" htmlEscape="false" maxlength="255" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">内容：</label>
			<div class="controls">
				<form:textarea path="content" htmlEscape="false" rows="4" class="input-xxlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group" >
			<label class="control-label">接收者：</label>
			<div class="controls" id="controls">
				<select id="optgroup_clickable" name="recevier" class="form-control form-control-chosen-optgroup" title="clickable_optgroup" data-placeholder="Please select..." multiple class="required">
					<optgroup label="所有人">
						<c:forEach items="${userList}" var="user">
						<option value="${user.openId}">${user.name}</option>
						</c:forEach>
					</optgroup>
						<c:forEach items="${stringListHashMap}" var="stringList" >
							<optgroup label="${stringList.key}">
								<c:forEach items="${stringList.value}" var="user" >
									<option value="${user.openId}">${user.name}</option>
								</c:forEach>
							</optgroup>
						</c:forEach>
				</select>

			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注信息：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255" class="input-xxlarge "/>
			</div>
		</div>
		<div class="form-actions">
			<shiro:hasPermission name="wechatmsg:weixinMsg:edit"><input id="btnSubmit" class="btn btn-primary" type="submit"  value="发 送" />&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
	<script src="${ctxStatic}/modules/chosen/js/jquery-1.11.0.min.js" type="text/javascript"></script>
	<script src="${ctxStatic}/modules/chosen/js/tether.min.js"></script>
	<script src="${ctxStatic}/modules/chosen/js/bootstrap.min.js"></script>
	<script src="${ctxStatic}/modules/chosen/js/chosen.jquery.js"></script>
	<script type="text/javascript">
        $('.form-control-chosen').chosen({
            allow_single_deselect: true,
            width: '100%'
        });
        $('.form-control-chosen-required').chosen({
            allow_single_deselect: false,
            width: '100%'
        });
        $('.form-control-chosen-search-threshold-100').chosen({
            allow_single_deselect: true,
            disable_search_threshold: 100,
            width: '100%'
        });
        $('.form-control-chosen-optgroup').chosen({
            width: '100%'
        });

        $(function() {
            $('[title="clickable_optgroup"]').addClass('chosen-container-optgroup-clickable');
        });
        $(document).on('click', '[title="clickable_optgroup"] .group-result', function() {
            var unselected = $(this).nextUntil('.group-result').not('.result-selected');
            if(unselected.length) {
                unselected.trigger('mouseup');
            } else {
                $(this).nextUntil('.group-result').each(function() {
                    $('a.search-choice-close[data-option-array-index="' + $(this).data('option-array-index') + '"]').trigger('click');
                });
            }
        });

       // $("#btnSubmit").click(function(){
       //
		//    console.log($("#optgroup_clickable").val());
       //
       // });

        // $(document).delegate("#optgroup_clickable_chosen","click",function(){
         //    $("#controls").css("height","300px");
		// })
		// $(document).delegate("input","blur",function(){
         //    var index=$("input").index(this);
         //    if(index==2){
         //        if($("#optgroup_clickable").val()==null){
         //            $("#controls").css("height","150px");
		// 		}
		// 	}
		// })
        $("#btnSubmit").click(function(){
            var str='';
            var arr=$("#optgroup_clickable").val();
            for(var i=0;i<arr.length;i++){
                str+=arr[i];
            }

        });
        /*接收者变化*/
        $("#optgroup_clickable").change(function(){
            // console.log($("#optgroup_clickable").val());
            var receiver=$("#optgroup_clickable").val();
            var arr=new Array();
            var span=[];
            for(var i in receiver){
                // console.log(i);
                // 遍历，把元素分别放入数组(不存在才放)
                    if(arr.indexOf(receiver[i])==-1){
                        arr.push(receiver[i]);
                    }
            }
		   var length=$("#optgroup_clickable").siblings(".chosen-container-optgroup-clickable").find("span").length;
			for(var i=0;i<length;i++){
				span.push($("#optgroup_clickable").siblings(".chosen-container-optgroup-clickable").find("span").eq(i).text());
			}
			var arr1=new Array();
			for(var i in span){
				// console.log(arr1.indexOf(span[i]));
				if(arr1.indexOf(span[i])!=-1){
					$("#optgroup_clickable").siblings(".chosen-container-optgroup-clickable").find("span").eq(i).parents("li").remove();
				}else{
					arr1.push(span[i]);
				}
			}
			// console.log(arr);
		// $("#optgroup_clickable").val(arr);
		// console.log($("#optgroup_clickable").val());
		})
	</script>
</body>
</html>



